/**
 * <p>Title: ServiceZMCT10.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-14 上午10:00:31  高强  Create
 *</pre>
 */
package com.baosight.sczx.zm.ct.service;
  
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.ct.DataInterface;
import com.baosight.sczx.common.zm.cInterface.ct.SystemSet;
import com.baosight.sczx.zm.ct.domain.ZMCTPerson;


/**
 * <p>Title: ServiceZMCT10.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-14 上午10:00:31  高强  Create
 *</pre>
 */
public class ServiceZMCT10 extends ServiceEPBase {
	
	/**
	 * 页面初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getDepartmentBlock("cmes.ct.department", false));//所在巷道
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		EiInfo outInfo = super.query(inInfo, "ZMCTPerson.query", new ZMCTPerson());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		boolean validateValue = validate(inInfo, "ZMCTPerson.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CT_DATAINTERFACE"));//主键
			EiInfo outinfo = super.insert(inInfo, "ZMCTPerson.insert");
			return outinfo;
		}
	}
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validateValue = validate(inInfo, "ZMCTPerson.validate", params);
		if(validateValue) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCTPerson.update");
			return outInfo;
		}
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo _temp = super.delete(inInfo, "ZMCTPerson.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 *在新增修改操作时工号不能重复 
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		
		params.put("fdNo", fdNo);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			StringBuffer msg = new StringBuffer("提交失败，");
			msg.append("工号重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 *人员信息公共选择用
	 * <p>Function: queryPerson</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public EiInfo queryPerson(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCTPerson.queryInterface", new ZMCTPerson());
		return outInfo;
	}
	
	/**
	 *导人员信息
	 * <p>Function: queryPerson</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public EiInfo importPerson(EiInfo inInfo) {
		
		String personDs = SystemSet.getInstance().getFdPersonDs();
		String personSql = SystemSet.getInstance().getFdPersonSql();
		if(excute(inInfo, personDs, personSql, "人员信息").getStatus()==-1)
		{
			EiInfo outInfo = this.initLoad(inInfo);
			return outInfo;
		}
		String departmentDs = SystemSet.getInstance().getFdDepartmentDs();
		String departmentSql = SystemSet.getInstance().getFdDepartmentSql();
		if(excute(inInfo, departmentDs, departmentSql ,"部门信息").getStatus()==-1)
		{
			EiInfo outInfo = this.initLoad(inInfo);
			return outInfo;
		}
		//excute(inInfo, departmentDs, departmentSql, "部门信息");
		EiInfo outInfo = this.initLoad(inInfo);
		return outInfo;
	}
	
	/**
	 *執行導入
	 * <p>Function: excute</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	private EiInfo excute(EiInfo inInfo, String dsId, String sql, String type)
	{
		Map inMap = new HashMap();
		DataInterface dataInterface = new DataInterface(dsId);
		EiBlock resultBlock = inInfo.getBlock(EiConstant.resultBlock);
		if(!dataInterface.drive())
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("找不到"+type+"驱动程序类 ，加载驱动失败");
			return inInfo;
		}
		if(!dataInterface.connect())
		{
			inInfo.setStatus(-1);
			inInfo.setMsg(type+"数据库连接失败");
			return inInfo;
		}
		sql = sql.replaceAll("\t", " ");
		sql = sql.replaceAll("\r", " ");
		sql = sql.replaceAll("\n", " ");
		ResultSet rs = dataInterface.search(sql);
		if(rs ==null)
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("请检查"+type+"sql语句的正确性");
			return inInfo;
		}
		 int i =0;
		 EiInfo outInfo = new EiInfo();
		 if(type.equals("人员信息"))
		 {
			 resultBlock.getRows().clear();
			 try{
				 while(rs.next())
				 {
					 String fdNo = rs.getString("no");
					 String fdName = rs.getString("name");
					 String fdMobilePhone = rs.getString("mobileNum");
					 String fdDepartmentId = rs.getString("departmentId");
					 
					 resultBlock.setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CT_PERSON"));
					 resultBlock.setCell(i, "fdNo", fdNo.trim());//工号
					 resultBlock.setCell(i, "fdName", fdName.trim());//姓名
		 			
					 if(fdMobilePhone == null)
					 {
						 resultBlock.setCell(i, "fdMobilePhone", null);//手机号码
					 }
					 else
					 {
						 resultBlock.setCell(i, "fdMobilePhone", fdMobilePhone.trim());//手机号码
					 }
					 
					 if(fdDepartmentId == null)
					 {
						 resultBlock.setCell(i, "fdDepartmentId", null);//手机号码
					 }else
					 {
						 resultBlock.setCell(i, "fdDepartmentId", fdDepartmentId.trim());//部门ID
					 }
		 			
					 i++;
				 }
			    }catch(SQLException e){
			    	inInfo.setStatus(-1);
					inInfo.setMsg("请检查"+type+"sql语句指定列是否存在");
					return inInfo;
		        } 
			 dao.delete("ZMCTPerson.deleteALL", inMap);
			 outInfo = super.update(inInfo,"ZMCTPerson.insert");
		 }else
		 {
			 resultBlock.getRows().clear();
			 try{
	
				 while(rs.next()){
					    String keyID = rs.getString("keyID");
					    String department = rs.getString("department");
			            resultBlock.setCell(i, "fdKeyId", keyID.trim());
			 			resultBlock.setCell(i, "fdDepartmentName", department.trim());//部门名称
			 			i++;
			         }
			    }catch(SQLException e){
			    	inInfo.setStatus(-1);
					inInfo.setMsg("请检查"+type+"sql语句指定列是否存在");
					return inInfo;
		        } 
			 dao.delete("ZMCTDepartment.deleteALL", inMap);   
			 outInfo = super.update(inInfo,"ZMCTDepartment.insert");
		 }
		if(!dataInterface.colseDs())
		 {
			 inInfo.setStatus(-1);
			 inInfo.setMsg(type+"数据库关闭连接失败");
		 }
		return outInfo;
	}
}

